rm(list = ls())
load( "clean_data/radpko_grid.RData")
load("clean_data/geo_pko.RData")
load("clean_data/geo_pko_tcc_long.RData")
load("clean_data/prio_static.RData")
library(sf)
library(tidyverse)
library(cshapes)
library(countrycode)
map <- cshp(date = as.Date("2015-07-01"), useGW = F)
map_lib <- map %>% dplyr::filter(country_name == "Liberia") %>% 
  st_transform(4326)
plot(map_lib["cowcode"])

grid_55 <- st_make_grid(map_lib, n = c(9, 8),
                        crs = 4326, what = 'polygons') 

tcc_map <- geo_pko_tcc_long %>% 
        dplyr::filter(country == "Liberia",
                      mission == "UNMIL",
                      date == as.Date("2004-05-01")) %>% 
  dplyr::mutate(iso_tcc = countrycode(cowcode, "cown", "iso3c")) 


load("PRIO/priogrid_cell.RData")
gid <- st_read("raw_data/priogrid_cellshp/priogrid_cell.shp")
priogrid_cell <- priogrid_cell %>% 
            dplyr::filter(gwno == 450) %>% 
            dplyr::select(gid, gwno) 
gid <- gid %>% dplyr::filter(gid %in% priogrid_cell$gid)
prio_static450 <- prio_static %>% 
        dplyr::select(id, row, col, xcoord, ycoord)
priogrid_cell450 <- left_join(priogrid_cell, prio_static450, by = c("gid" = "id"))
# check how many gid has 
grids <- unique(intersect(tcc_map$prioid, priogrid_cell450$gid))


## figure1

tcc_map <- left_join(tcc_map, priogrid_cell450, by = c("prioid" = "gid"))%>% 
  dplyr::filter(!is.na(gwno))
## make it a sf
tcc_map <- st_as_sf(tcc_map, coords = c("xcoord", "ycoord"), 
                    crs = st_crs(map_lib))

library(ggrepel)
library(ggthemes)
library(purrr)
library(sf)
tcc_map <-tcc_map %>% dplyr::mutate(centroid = map(geometry, st_centroid),
                                  coords = map(centroid, st_coordinates), coords_x = map_dbl(coords, 1),
                                  coords_y = map_dbl(coords, 2))

priogrid_cell450 <- st_as_sf(priogrid_cell450,coords = c("xcoord", "ycoord"), 
                             crs = st_crs(map_lib) )

p1 <- ggplot() + geom_sf(data = gid,fill = 'white', lwd = 0.1)+
  geom_sf(data = map_lib, fill = 'transparent', lwd = 0.5) +
  theme_map()+
  geom_point(data = tcc_map, aes(x= coords_x, y = coords_y), size = 1.5, shape = 16)+
  geom_text_repel(data = tcc_map,aes(x= coords_x, y = coords_y,
                                     label =iso_tcc),size = 2,
                  max.overlaps = Inf ,
                  min.segment.length = 0)+
  coord_sf()+ 
  labs(title = "",
       subtitle = "",
       y = "", x = "")+
  theme(line = element_blank(),rect = element_blank(), #defien the margin line
        axis.text = element_blank(), axis.title = element_blank(), 
        panel.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), axis.ticks.length = unit(0,"cm"),
        panel.spacing = unit(0, "lines"), 
        plot.title = element_text(hjust = 0.5),
        text = element_text(size=16),
        plot.margin = unit(c(t =-2,r = 0, b = 0, l = 0), "lines")) 


tcc_map <- geo_pko_tcc_long %>% 
  dplyr::filter(country == "Liberia",
                mission == "UNMIL",
                date == as.Date("2005-03-01")) %>% 
  dplyr::mutate(iso_tcc = countrycode(cowcode, "cown", "iso3c")) 

load("PRIO/priogrid_cell.RData")
priogrid_cell <- priogrid_cell %>% 
  dplyr::filter(gwno == 450) %>% 
  dplyr::select(gid, gwno) 
prio_static450 <- prio_static %>% 
  dplyr::select(id, row, col, xcoord, ycoord)
priogrid_cell450 <- left_join(priogrid_cell, prio_static450, by = c("gid" = "id"))
# check how many gid has 
grids <- unique(intersect(tcc_map$prioid, priogrid_cell450$gid))

## figure1

tcc_map <- left_join(tcc_map, priogrid_cell450, by = c("prioid" = "gid"))%>% 
  dplyr::filter(!is.na(gwno))
## make it a sf
tcc_map <- st_as_sf(tcc_map, coords = c("xcoord", "ycoord"), 
                    crs = st_crs(map_lib))

library(ggrepel)
library(ggthemes)
library(purrr)
library(sf)
tcc_map <-tcc_map %>% dplyr::mutate(centroid = map(geometry, st_centroid),
                                    coords = map(centroid, st_coordinates), coords_x = map_dbl(coords, 1),
 
priogrid_cell450 <- st_as_sf(priogrid_cell450,coords = c("xcoord", "ycoord"), 
                             crs = st_crs(map_lib) )

p2 <- ggplot() + geom_sf(data = gid,fill = 'white', lwd = 0.1)+
  geom_sf(data = map_lib, fill = 'transparent', lwd = 0.5) +
  theme_map()+
  geom_point(data = tcc_map, aes(x= coords_x, y = coords_y), size = 1.5, shape = 16)+
  geom_text_repel(data = tcc_map,aes(x= coords_x, y = coords_y,
                                     label =iso_tcc),size = 2,
                  max.overlaps = Inf ,
                  min.segment.length = 0)+
  coord_sf()+ 
  labs(title = "",
       subtitle = "",
       y = "", x = "")+
  theme(line = element_blank(),rect = element_blank(), #defien the margin line
        axis.text = element_blank(), axis.title = element_blank(), 
        panel.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), axis.ticks.length = unit(0,"cm"),
        panel.spacing = unit(0, "lines"), 
        plot.title = element_text(hjust = 0.5),
        text = element_text(size=16),
        plot.margin = unit(c(t =-2,r = 0, b = 0, l = 0), "lines")) 

library(grid)
library(gridExtra)
m1 <- grid.arrange(p1, p2, nrow=1)
ggsave("map.png", m1, width = 8, height = 5)

library(cowplot)
pdf("map.pdf", width = 8, height = 5)
plot_grid(p1, p2)
dev.off()

